package com.gitee.chili.leisure.message;

import com.gitee.chili.leisure.ResponseCode;
import com.gitee.chili.leisure.ResponseResult;
import com.gitee.chili.leisure.RoomManager;
import com.gitee.chili.leisure.SimpleBeanFactory;
import com.gitee.chili.leisure.core.AbstractDefaultMessage;
import com.gitee.chili.leisure.core.Packet;
import com.gitee.chili.leisure.domain.dto.EntryRoomQuery;
import com.gitee.chili.leisure.exception.RoomException;
import com.gitee.chili.leisure.game.RoomDetail;
import com.gitee.chili.leisure.model.HallModel;
import com.gitee.chili.leisure.model.MessageCmdEnum;
import com.gitee.chili.leisure.service.SpecificTaskTransferClient;
import com.gitee.chili.leisure.util.AuxiliaryChannel;
import com.gitee.chili.leisure.util.AuxiliaryRoom;
import com.gitee.chili.leisure.util.Connection;
import com.gitee.chili.leisure.util.MemberChannel;
import com.google.protobuf.InvalidProtocolBufferException;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.Objects;

/**
 * EntryRoom
 *
 * @author week solider
 * @since 2020.09.04
 */
@Slf4j
public class EntryRoom extends AbstractDefaultMessage {

    private Long roomNum;

    public EntryRoom(Packet packet, Channel channel) {
        super(packet, channel);
    }

    @Override
    protected void decoder(byte[] body) throws InvalidProtocolBufferException {

        HallModel.EntryRoomCmd entryRoom = HallModel.EntryRoomCmd.parseFrom(body);

        roomNum = getRoomNum();

        RoomDetail room = RoomManager.obtain(roomNum);

        if (Objects.isNull(room)) {
            AuxiliaryChannel.errorMessage(getChannel(), getPacket().getCmd(), ResponseCode.NOT_FOUND_ROOM);
            throw new RoomException("房间不存在");
        }

        if(room.getPlayers().size() >= room.getPeopleNum()){
            AuxiliaryChannel.errorMessage(getChannel(), getPacket().getCmd(), ResponseCode.ROOM_FULL);
            throw new RoomException("房间人数已满");
        }

        SpecificTaskTransferClient transferClient = SimpleBeanFactory.getBean(SpecificTaskTransferClient.class);

        EntryRoomQuery query = EntryRoomQuery.builder()
                .gameType(entryRoom.getGameType())
                .roomNum(entryRoom.getRoomNum())
                .build();

        ResponseResult<com.gitee.chili.leisure.domain.EntryRoom> result = transferClient.entryRoom(query);

        asserts(result);

        // 添加到信道组中
        MemberChannel memberChannel = MemberChannel.builder()
                .memberId(getMemberId())
                .channel(getChannel())
                .build();

        Connection.broadcaster().set(roomNum, memberChannel);

    }

    @Override
    public Packet parseBody() {

        getChannel().attr(AttributeKey.valueOf("roomNum")).set(roomNum);

        // 获取IP地址
        InetSocketAddress address = (InetSocketAddress) getChannel().remoteAddress();
        String ip = address.getAddress().getHostAddress();

        RoomDetail roomDetail = RoomManager.obtain(roomNum);

        HallModel.NotifyRoomResult.Builder builder = AuxiliaryRoom.notifyRoom(roomDetail, ip);

        return Packet.newPacket(MessageCmdEnum.Command.NOTIFY_ROOM_INFO, builder.build().toByteArray());
    }
}
